Imports System.Data.OleDb

Public Class frmThemPhieuKho
    Public LoaiPhieu As String
    Public LoaiPhieuID As Integer = -1
    Private Sub frmThemPhieuKho_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            cbxTuTang.Checked = False
            If LoaiPhieuID > -1 Then
                LoaiPhieu_LoadByID(LoaiPhieuID)
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub LoaiPhieu_LoadByID(ByVal _LoaiPhieuID As Integer)
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtPhieuKho As New DataTable
            Dim query As String = "Select LoaiPhieuID,TenPhieu,GhiChu,SoPhieuTuTang From LoaiPhieu Where Xoa=0 And LoaiPhieuID='" & _LoaiPhieuID & "'"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtPhieuKho)
            dbConn.Close()
            If dtPhieuKho.Rows.Count > 0 Then
                txtTenPhieu.Text = dtPhieuKho.Rows(0)("TenPhieu")
                txtGhiChu.Text = dtPhieuKho.Rows(0)("GhiChu")
                LoaiPhieu = dtPhieuKho.Rows(0)("LoaiPhieu")
                cbxTuTang.Checked = IIf(dtPhieuKho.Rows(0)("SoPhieuTuTang") = 1, True, False)
            Else
                txtTenPhieu.Text = ""
                txtGhiChu.Text = ""
                cbxTuTang.Checked = False
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub cmdLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLuu.Click
        Try
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            'luu thong tin phieu kho
            Dim TenPhieu As String = txtTenPhieu.Text
            Dim GhiChu As String = txtGhiChu.Text
            Dim SoPhieuTuTang As Integer = IIf(cbxTuTang.Checked, 1, 0)
            If LoaiPhieuID > -1 Then
                query = String.Format("Update LoaiPhieu Set TenPhieu=N'{0}',GhiChu=N'{1}',NgaySua=N'{2}',NguoiSua=N'{3}',SoPhieuTuTang={4} Where LoaiPhieuID={5}", TenPhieu, GhiChu, NgayTao, NguoiTao, SoPhieuTuTang, LoaiPhieuID)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                txtTenPhieu.Text = ""
                txtGhiChu.Text = ""
                Me.Close()
            Else
                query = String.Format("Insert Into LoaiPhieu(TenPhieu,LoaiPhieu,GhiChu,NgayTao,NguoiTao,SoPhieuTuTang) Values(N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',{5})", TenPhieu, LoaiPhieu, GhiChu, NgayTao, NguoiTao, SoPhieuTuTang)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                txtTenPhieu.Text = ""
                txtGhiChu.Text = ""
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHuyBo.Click
        Try
            txtTenPhieu.Text = ""
            txtGhiChu.Text = ""
            LoaiPhieu = ""
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub

    
End Class